/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.phonebook;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.phonebook.Phonebook;
import com.unis.service.phonebook.PhonebookService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-01-31
 */
@Controller
@RequestMapping("/phonebook")
public class PhonebookController {

    private PhonebookService phonebookService;

    /****************************************
     * Insert a instace of Phonebook
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertPhonebook(@RequestBody Phonebook phonebook) throws Exception {
        phonebook.setStatmentId("Phonebook.insert");
        String status = "failure";
        phonebookService.insert(phonebook);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Phonebook
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Phonebook phonebook) throws Exception {
        phonebook.setStatmentId("Phonebook.update");
        String status = "failure";
        phonebookService.update(phonebook);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Phonebook
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Phonebook phonebook) throws Exception {
        phonebook.setStatmentId("Phonebook.delete");
        String status = "failure";
        phonebookService.batchDelete(phonebook);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Phonebook
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Phonebook phonebook = new Phonebook();
        phonebook.setStatmentId("Phonebook.queryAsList");
        BeanUtils.populate(phonebook, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(phonebook, pageMap);
        List<BaseModel> phonebookList = phonebookService.queryAsList(phonebook);
        Map<String, Object> renderMap = GridUIMapper.render(phonebook, phonebookList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Phonebook
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Phonebook phonebook) throws Exception {
        phonebook.setStatmentId("Phonebook.queryAsObject");
        return phonebookService.queryAsObject(phonebook);
    }

    @RequestMapping("/page")
    public String forword() {
        return "phonebook/phonebookList";
    }

    @Resource
    public void setPhonebookService(PhonebookService phonebookService) {
        this.phonebookService = phonebookService;
    }
}
